package com.zuoyy.common.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class DateUtil {

	/**
	 * 使用预设格式将字符串转为Date yyyy-MM-dd HH:mm:ss
	 */
	public static Date parse(String strDate, String format) throws Exception {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.parse(strDate);
	}

	/**
	 * 格式化时间
	 * 
	 * @author zuopeng
	 * @param date
	 * @param format
	 *            yyyy-MM-dd HH:mm:ss
	 * @return
	 */
	public static String formatDate(Date date, String format) {
		DateFormat df = new SimpleDateFormat(format);
		String s = df.format(date);
		return s;
	}

	/**
	 * 判断日期什么有效
	 * 
	 * @author zuopeng
	 * @param beginDate
	 * @return
	 * @throws ParseException
	 */
	public static boolean isEffectiveBegin(String beginDate) throws ParseException {
		boolean flag = true;
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		Calendar day = Calendar.getInstance();
		Calendar c1 = Calendar.getInstance();
		day.setTime(df.parse(df.format(new Date())));
		c1.setTime(df.parse(beginDate));
		int r1 = day.compareTo(c1);
		if (r1 < 0) {
			flag = false;
		}
		return flag;
	}

	public static boolean isEffectiveEnd(String endDate) throws ParseException {
		boolean flag = true;
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		Calendar day = Calendar.getInstance();
		Calendar c1 = Calendar.getInstance();
		day.setTime(df.parse(df.format(new Date())));
		c1.setTime(df.parse(endDate));
		int r1 = day.compareTo(c1);
		if (r1 > 0) {
			flag = false;
		}
		return flag;
	}

	/**
	 * 获取当前时间
	 * 
	 * @author zuopeng
	 * @return
	 */
	public static long getTime() {
		Date d = new Date();
		return d.getTime();
	}

	public static String transferLongToDate(String dateFormat, Long millSec) {
		SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
		Date date = new Date(millSec);
		return sdf.format(date);
	}

	/**
	 * 
	* @Description: TODO
	* @author zuo
	* @param DATE1
	* @param dayDate
	* @param dateFormat
	* @return
	 */
	public static int compare_date(String DATE1, String dayDate,String dateFormat) {
		DateFormat df = new SimpleDateFormat(dateFormat);
		try {
			Date dt1 = df.parse(DATE1);
			Date dt2 = df.parse(dayDate);
			if (dt1.getTime() > dt2.getTime()) {
				return 1;
			} else if (dt1.getTime() < dt2.getTime()) {
				return -1;
			} else {
				return 0;
			}
		} catch (Exception exception) {
			exception.printStackTrace();
		}
		return 0;
	}
	
	public static List<String> getMonthBetween(String minDate, String maxDate) throws ParseException {
	    List<String> result = new ArrayList<String>();
	    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");//格式化为年月

	    Calendar min = Calendar.getInstance();
	    Calendar max = Calendar.getInstance();

	    min.setTime(sdf.parse(minDate));
	    min.set(min.get(Calendar.YEAR), min.get(Calendar.MONTH), 1);

	    max.setTime(sdf.parse(maxDate));
	    max.set(max.get(Calendar.YEAR), max.get(Calendar.MONTH), 2);

	    Calendar curr = min;
	    while (curr.before(max)) {
	     result.add(sdf.format(curr.getTime()));
	     curr.add(Calendar.MONTH, 1);
	    }
	    if(result.size()>12){
	    	result = result.subList(0, result.size()-1);
	    }
	    return result;
	  }
	
	public static Date before7day(){
	    Calendar c = Calendar.getInstance();  
	    c.add(Calendar.DAY_OF_MONTH, -7);
	    return c.getTime();
	}
	
	public static Date before30day(){
		Calendar c = Calendar.getInstance();  
	    c.add(Calendar.MONTH, -1);
	    return c.getTime();
	}

	public static Date addDate(Date date ,Integer year,Integer month){
		Calendar calendar   =   new GregorianCalendar();
		calendar.setTime(date);
		if(year!=null){
			calendar.add(calendar.YEAR, year);//把日期往后增加一年.整数往后推,负数往前移动
		}
		if(month!=null){
			calendar.add(calendar.MONTH, month);
		}
		return calendar.getTime();
	}

	public static Date nowDate0h0m0s(){
		Calendar c = Calendar.getInstance();
		c.set(Calendar.HOUR_OF_DAY, 0);
		c.set(Calendar.MINUTE, 0);
		c.set(Calendar.SECOND, 0);
		c.set(Calendar.MILLISECOND, 0);
		return c.getTime();
	}

	public static String getLastMonth(Date date) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date); // 设置为当前时间
		calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) - 1); // 设置为上一个月
		date = calendar.getTime();
		String accDate = format.format(date);
		return accDate;
	}

}
